package com.buyouchina.web.action.privilege;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.buyouchina.bean.PageView;
import com.buyouchina.bean.privilege.User;
import com.buyouchina.service.privilege.UserService;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
@Controller("UserList")
public class UserListAction extends BaseAction implements ModelDriven<User>{
	private static final long serialVersionUID = 2554530057355903464L;
	private User user = new User();
	@Resource(name="userServiceBean") private UserService userService;
	@Permission(model="user",privilegeValue="view")
	public String execute() throws Exception {
		PageView<User> pageView = new PageView<User>(12,this.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("realname", "desc");
		if("true".equals(this.getQuery())){
			StringBuilder sb = new StringBuilder();
			List<Object> params = new ArrayList<Object>();
			if(user.getPhone()!=null && !"".equals(user.getPhone().trim())){
				sb.append(" o.phone=?").append(params.size()+1);
				params.add(user.getPhone());
			}
			if(user.getRealname()!=null && !"".equals(user.getRealname().trim())){
				if(params.size()>0)  sb.append(" and ");
				sb.append(" o.realname like ?").append(params.size()+1);
				params.add("%"+user.getRealname()+"%");
			}
			pageView.setQueryResult(
					userService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(),
							sb.toString(), params.toArray(), orderby));
			
		}else{
			pageView.setQueryResult(userService.getScrollData(pageView.getFirstResult(), pageView.getMaxresult(), orderby));
		}
		request.setAttribute("pageView", pageView);
		return SUCCESS;
	}
	public User getModel() {
		return user;
	}
	
}
